const service = require('./../service');
const logger = require('./../commons/logger');

exports = module.exports = {
  myInfo: async (ctx, next) => {

    let params = ctx.params || {};

    logger.info('myInfo', params);
    try {
      if (params.code && params.code.length > 0) {
        let ret = await service.getUserInfo(ctx, params.code);
        if (ret.code !== 10000) {
          throw new BSPError(ret);
        } else {

          let result = ret.data;
          logger.debug('myInfo', result);
          ctx.status = 200;
          ctx.body = { code: 10000, msg: '获取用户信息成功', data: result };
        }
      } else {
        ctx.status = 200;
        ctx.body = {
          code: 10000,
          msg: '获取用户信息成功',
          data: {}
        };
      }
    } catch (e) {
      logger.error('myInfo Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
  taskList: async (ctx, next) => {

    let params = ctx.params || {};

    logger.info('taskList', params);
    try {
      let result = {
        code: 10000,
        msg: '获取用户信息成功',
        data: {
          page: {
            number: 0,
            size: 20,
            total: 0
          },
          data: []
        }
      };

      if (params.code && params.code.length > 0) {
        logger.info('params.code',params.code);                                                            
        let person = await service.getUserInfo(ctx, params.code);
        logger.info('1',person);
        if (person.code === 10000) {
          person.data.personId = '332738903c5311e7bbede1241e7c3936';
          if (person.data && person.data.personId && person.data.personId.length > 0) {
            ctx.session.task = {
              q: {
                page: {
                  size: 20,
                  number: 0
                },
                query: {
                  personId: person.data.personId
                }
              }
            }
            let record = await service.getMyTask(ctx, ctx.session.task);
            if (record.code !== 10000) {
              throw new BSPError(record);
            } else {
              logger.debug('taskList', record.data);
              result = record;
            }
          }
        }
      }

      ctx.status = 200;
      ctx.body = result;
    } catch (e) {
      logger.error('taskList Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
  moreTask: async (ctx, next) => {
    let query = ctx.session.task || {};

    logger.info('moreTask', query);
    try {
      query.q.page.number = query.q.page.number + 1;
      let tasks = await service.getMyTask(ctx, query);
      if (tasks.code !== 10000) {
        throw new BSPError(tasks);
      } else {
        logger.debug('moreTask', tasks.data);
        ctx.status = 200;
        ctx.body = { code: 10000, msg: '获取用户信息成功', data: tasks.data };
      }
    } catch (e) {
      logger.error('moreTask Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
  confirmTask: async (ctx) => {
    let enlistId = ctx.request.body;
    logger.info('confirmTask', enlistId);
    try {
      let ret = await service.confirmTask(ctx, enlistId);
      if (ret.code !== 10000) {
        throw new BSPError(ret);
      } else {
        logger.debug('confirmTask', ret.data);
        ctx.status = 200;
        ctx.body = { code: 10000, msg: '确认学习计划成功', data: ret.data };
      }
    } catch (e) {
      logger.error('confirmTask Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  },
  cancelTask: async (ctx) => {
    let enlistId = ctx.request.body;
    logger.info('cancelTask', enlistId);
    try {
      let ret = await service.cancelTask(ctx, enlistId);
      if (ret.code !== 10000) {
        throw new BSPError(ret);
      } else {
        logger.debug('cancelTask', ret.data);
        ctx.status = 200;
        ctx.body = { code: 10000, msg: '取消学习计划成功', data: ret.data };
      }
    } catch (e) {
      logger.error('cancelTask Error', e);
      ctx.status = 500;
      ctx.body = { code: e.name, msg: e.message };
    }
  }
}
